#the code below is to generate Figure 2 in the main text

setwd("C:/Users/lps29/Dropbox/Party_Interactions_Perceptions/PSRM Submission/Replication Files")

rm(list=ls())


library(haven)
dat <- read_dta("replication_data.dta")

library(lme4)


#create group based on habitual news reception 

dat$pkgroup[(dat$sumpk<3)& (dat$countryyear==1|dat$countryyear==2)]<-1
dat$pkgroup[(dat$sumpk==3|dat$sumpk==4)&(dat$countryyear==1|dat$countryyear==2)]<-2
dat$pkgroup[(dat$sumpk>4)&(dat$countryyear==1|dat$countryyear==2)]<-3

dat$pkgroup[(dat$sumpk<4)& (dat$countryyear==3)]<-1
dat$pkgroup[(dat$sumpk==4)&(dat$countryyear==3)]<-2
dat$pkgroup[(dat$sumpk>4)&(dat$countryyear==3)]<-3

dat$pkgroup[(dat$sumpk<2)& (dat$countryyear==4)]<-1
dat$pkgroup[(dat$sumpk==2|dat$sumpk==3)&(dat$countryyear==4)]<-2
dat$pkgroup[(dat$sumpk>3)&(dat$countryyear==4)]<-3

dat$pkgroup[(dat$sumpk<3)& (dat$countryyear==5)]<-1
dat$pkgroup[(dat$sumpk==3|dat$sumpk==4)&(dat$countryyear==5)]<-2
dat$pkgroup[(dat$sumpk>4)&(dat$countryyear==5)]<-3

dat$pkgroup[(dat$sumpk<3)& (dat$countryyear==6)]<-1
dat$pkgroup[(dat$sumpk==3|dat$sumpk==4)&(dat$countryyear==6)]<-2
dat$pkgroup[(dat$sumpk>4)&(dat$countryyear==6)]<-3

dat$pkgroup[(dat$sumpk<2)& (dat$countryyear==7)]<-1
dat$pkgroup[(dat$sumpk==2|dat$sumpk==3)&(dat$countryyear==7)]<-2
dat$pkgroup[(dat$sumpk>3)&(dat$countryyear==7)]<-3


dat$pkgroup<-as.factor(dat$pkgroup)

dat$pkgroup <- factor(dat$pkgroup,
                           levels = c("1","2","3"),
                           labels = c("Low Habitual News Reception",
                                      "Medium Habitual News Reception"
                                      ,"High Habitual News Reception"))


model<- lmer(coop ~ coopscore_meanfull*pkgroup +
               mf_df_rile1+ as.factor(current_bothCab)+
               (1 | groupdyad)+(1|respid), data =dat)



library(sjPlot)
library(ggplot2)
library(ggpubr)

theme_set(theme_pubr())



plot_model(model,type="int", grid=T,title=(""))+
  theme(text=element_text(size=12))+labs(x = "Cooperation/Conflictual Events Score")+
  labs(y="Perceived Levels of Cooperation")

ggsave("fig2.pdf",width=10, height=8)

